package com.bf.xpdd.persistent.sqlmapdao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bf.xpdd.domain.ActivitySettingBean;
import com.bf.xpdd.persistent.iface.DaoActivitySetting;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * 活动参数模板DAO的实现类
 * 
 * @author linmu
 * 
 */
public class DaoActivitySettingImpl implements DaoActivitySetting
{

	private Logger logger = Logger.getLogger(DaoActivitySettingImpl.class);

	/**
	 * ibatis的查询接口
	 */
	private SqlMapClient sqlMapClient;

	/**
	 * 注入sqlMapClient
	 * 
	 * @param sqlMapClient
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	@Override
	public void addActivitySetting(ActivitySettingBean actSetting)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.insert("activitySetting.addActSettingBean", actSetting);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void delActivitySetting(long id, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("nick", nick);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activitySetting.delActSettingBeanById", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivitySetting(ActivitySettingBean actSetting)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activitySetting.modifyActSettingBean", actSetting);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<ActivitySettingBean> queryAllActSetting(String nick)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivitySettingBean> list = (List<ActivitySettingBean>) this.sqlMapClient
					.queryForList("activitySetting.queryAllActSettingBean", nick);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public ActivitySettingBean queryActSettingById(long id)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			// List<ActivitySettingBean> list = (List<ActivitySettingBean>)
			// this.sqlMapClient
			// .queryForList("activitySetting.queryActSettingBeanById", id);
			Object obj = this.sqlMapClient.queryForObject(
					"activitySetting.queryActSettingBeanById", id);
			this.sqlMapClient.commitTransaction();

			return (ActivitySettingBean) obj;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivitySettingBean> queryActSettingByIdList(List<Long> idList)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivitySettingBean> list = (List<ActivitySettingBean>) this.sqlMapClient
					.queryForList("activitySetting.queryActSettingByIdList", idList);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivitySettingBean> querySettingsByIsUsed(boolean isUsed, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isUsed", isUsed);
		map.put("nick", nick);
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivitySettingBean> list = (List<ActivitySettingBean>) this.sqlMapClient
					.queryForList("activitySetting.querySettingsByIsUsed", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

}
